// yc@2009-10-22 17:25:02

function getRandomColor()
{
    // colors form http://www.alexc.cn/post/62.html
    var colors = [  'FF7400', '4096EE', 'FF0084', '73880A', '356AA0',
                    '36393D', 'B02B2C', '000000', 'D15600', 'C79810', 
                    '6BBA70', '3F4C6B', 'D01F3C', 'FF1A00', '008C00',
                    '006E2E'];
    var size = colors.length;
    getRandomColor = function(){
        var index = Math.floor(Math.random() * size);
        return colors[index];
    };
    return getRandomColor();
}
function ajaxGet(url, params, cbFunc)
{
    var func = (typeof(cbFunc) == 'undefined') ? Prototype.emptyFunction : cbFunc;
    new Ajax.Request(url, {
        'method': 'get',
        'parameters': $H(params),
        'onSuccess': func
    });
}
function deleteBlog(span)
{
    if (!confirm('Really?'))
        return;
    var div = $(span).up('div.blog');
    if (!div)
        return;
    var url = '/blog/del/id/' + div.readAttribute('blogid');
    ajaxGet(url, null, function(xhr){
        if (xhr.responseText == 'OK')
        div.remove();
    });
}
function markBlog(span, set)
{
    var div = $(span).up('div.blog');
    if (!div)
        return;
    var url = '/blog/mark/id/' + div.readAttribute('blogid') + '/set/';
    url += set ? 'true' : 'false';
    ajaxGet(url, null, function(xhr){
        if (xhr.responseText == 'OK'){
            var info = div.down('span.info');
            var i = info.innerHTML.stripTags();
            if (set)
                info.update('<del>' + i + '</del>' + $('operation').innerHTML);
            else
                info.update(i + $('operation').innerHTML);
            var color = div.hasClassName('even')? '#EEEEEE' : '#FFFFFF';
            new Effect.Highlight(div, {startcolor:'#FF7400', restorecolor:color, duration:0.4, afterFinish:function(){div.removeAttribute('style')}});
        }
    });
}
function insertTag(a)
{
    var tags = $('blogTag').value;
    if (tags == '')
        tags = a.innerHTML;
    else if (!tags.include(a.innerHTML))
        tags += ',' + a.innerHTML;
    $('blogTag').value = tags;
    hideTagSuggest();
}

function showTagSuggest()
{
    $('suggestTags').show();
    $('blogTag').setStyle({'borderLeft':'1px solid gray', 'borderRight': '1px solid gray'});
}

function hideTagSuggest()
{
    $('suggestTags').hide();
    $('blogTag').setStyle({'borderLeft':'1px solid transparent', 'borderRight': '1px solid transparent'});
}

function placeElements()
{
    var pageHeight = $$('body')[0].getHeight();
    if ($('blogs')){
        var blogHeight = pageHeight - $('menu').getHeight() - $('tagline').getHeight() - $('submitForm').getHeight() - 90;
        $('blogs').setStyle({height:blogHeight + 'px'});
    }
    if ($('tags')){
        var tagsHeight = pageHeight - $('menu').getHeight() - 100;
        if ($('tags').getHeight() > tagsHeight)
            $('tags').setStyle({height:tagsHeight + 'px'});
    }
}

function colorTags(container)
{
    if (typeof(container) != 'string' || !$(container))
        return;
    $$('#' + container + ' a.tag').each(function(a){a.setStyle({color: '#' + getRandomColor()});});
    Event.observe($(container), 'mouseover', function(event){
        var a = $(Event.findElement(event, 'A'));
        if (!a || !a.hasClassName('tag'))
            return;
        a.setStyle({color: '#FFF', background: a.getStyle('color')});
    });
    Event.observe($(container), 'mouseout', function(event){
        var a = $(Event.findElement(event, 'A'));
        if (!a || !a.hasClassName('tag'))
            return;
        a.setStyle({color: a.getStyle('backgroundColor'), background: 'transparent'});
    });
}

Element.observe(window, 'load', function(){
    ['tagline', 'blogs', 'suggestTags', 'tags'].each(colorTags);
    Ajax.Responders.register({
        onCreate: function() {if ($('ajaxMsg')) $('ajaxMsg').setStyle({visibility:'visible'});},
        onComplete: function() {if ($('ajaxMsg')) $('ajaxMsg').setStyle({visibility:'hidden'});},
        onException: function() {alert('ajax loading error!')},
        onFailure: function() {alert('ajax loading error!')} 
    });
    placeElements();
});
Element.observe(window, 'resize', placeElements);
